﻿using System;
using System.Windows.Forms;

namespace RGBGame.VisualJSApp
{
    public partial class MainForm : VisualJS.Web.Forms.Form
    {

#region Constructors
        public MainForm()
        {
            InitializeComponent();
            AfterInitialization();
        } 

        //Use the below constructor if you create the instance of this Form object other than the active Thread
        //otherwise you will receive "Cant locate the ClientID in active context. Consider using Form constructor with ClientID parameter" exception
        public MainForm(string clientId): base(clientId)
        {
            InitializeComponent(); 
            AfterInitialization();
        }
#endregion


        internal GameTools game, aiPlayer;
        //Use below method for the tasks after the initialization of the Form
        void  AfterInitialization()
        {
            game = new GameTools(this);

            if (this.IsTouchEnabled)
                lnkBan.Visible = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //VisualJS.NET tries the form objects for once before converting them into client. On this trial, the ClientID is always -1
            if (this.ClientID == "-1")
                return;

            Utility.BindSession(this.SessionID, this.ClientID);

            game.DrawTiles();
            label2.Visible = false;
            label1.ZOrder = 1;
        }


        void PlayAlone()
        {
            Room.RemovePlayer(ClientID);
            aiPlayer = new GameTools(this, true);

            Room.CreateGame("AI_" + ClientID, aiPlayer.GameEvent, ClientID, game.GameEvent);
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            game.updateSizes();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Room.RemovePlayer(ClientID, game.opponentsId);
        }

        public void lblSingle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PlayAlone();
        }

        private void lblTut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new frmTutorial().ShowDialog();
        }

        private void lblMulti_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Room.AddPlayer(this.ClientID, game.GameEvent);
            game.highlightInfo("Waiting for another player to play..", true, false);
        }
    }
}